/*
 * @Descripttion: 
 * @version: 
 * @Author: 
 * @Date: 2022-04-25 11:40:02
 * @LastEditors: weidelong
 * @LastEditTime: 2022-05-09 15:01:44
 */
import { createRouter,createWebHashHistory } from 'vue-router'
import Home from '../views/Home.vue'

const routes = [
    {
        path: '/',
        redirect: '/login'
    },
    {
        path: '/home',
        redirect: '/home/index'
    },
    {
        path: '/',
        name: 'Home',
        component: Home
    },
    {
        path: '/login',
        name: 'Login',
        component: () => import('../views/login/login')
    },
    {
        path: '/home',
        name: 'Home',
        component: Home,
        children: [
			// 首页
            {
                path: '/home/index',
                name: 'Index',
                component: () => import('../views/index/index.vue'),
            },
			// 添加文章
            {
                path: '/home/manage/addNewArticle',
                name: 'addNewArticle',
                component: () => import('../views/articleMange/addNewArticle.vue'),
            },
            // 空白页
            {
                path: '/home/manage/none',
                name: 'none',
                component: () => import('../views/articleMange/none.vue'),
            },
			// 文章审核
            {
                path: '/home/manage/audit',
                name: 'Audit',
                component: () => import('../views/articleMange/audit.vue'),
            },
			// 文章详情
			{
			    path: '/home/manage/detail',
			    name: 'Detail',
			    component: () => import('../views/articleMange/detail.vue'),
			},
            //医师文章
            {
                path: '/home/manage/myArticle',
                name: 'MyArticle',
                component: () => import('../views/articleMange/myArticle.vue'),
            },
			// 用户管理
			{
			    path: '/home/userMage/userMage',
			    name: 'UserMage',
			    component: () => import('../views/userMage/userMage.vue'),
			},
			// 医师管理
            {
			    path: '/home/userMage/doctorMage',
			    name: 'DoctorMage',
			    component: () => import('../views/userMage/doctorMage.vue'),
			},
            // 移动端管理
            {
			    path: '/home/userMage/mobileUser',
			    name: 'MobileUser',
			    component: () => import('../views/userMage/moblieUser.vue'),
			},
			// 消息
            {
                path: '/home/message',
                name: 'message',
                component: () => import('../views/message/message.vue')
            }
        ]
    },
    
]

const router = createRouter({
    history: createWebHashHistory(),
    routes
})

router.beforeEach((to, from, next) => {
    // 如果访问的是登录页面
    if (to.path === '/login') return next()
    // 如果访问的不是登录页面，则要先检查是否有token
    const tokenStr = localStorage.getItem('token')
    // 如果没有token
    if (!tokenStr) return next('/login')
    // 如果有token
    next()
})

router.beforeEach((to, from, next) => {
    if (from.path === '/home/message') {
        // console.log('离开消息')
    }
    next()
})

export default router
